#!/usr/bin/python
# -*- coding: utf8 -*-

import urllib2
import urllib
import string
import re
import threading

# init_url='http://www.qiushibaike.com/hot/page/'
init_url='http://www.qiushibaike.com/late/page/'
items = []
page_num = 1
loading = False
needShow = False

def show():
	global needShow
	if (not needShow):
		return

	size = len(items)
	if size>10 :
		size = 10
	print 'show size=',size
	for i in range (1,size):
		item = items.pop(0)
		print item[0].replace('\n','')
		print item[1].replace('\n',''),'\n'

	if size == 10:
		needShow = False

def fetch_html(url):
	global loading
	loading = True
	user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
	headers = { 'User-Agent' : user_agent }
	req = urllib2.Request(url, headers=headers)
	res = urllib2.urlopen(req)
	txt = res.read()
	myItems = re.findall('<div\s+class="content"\s+title="(.*?)">(.*?)</div>',txt,re.S)
	for item in myItems:
		items.append(item)
	print 'load done'
	loading = False
	show()

def load():
	global page_num
	print '=====================>>>> load,page=',page_num
	curr_url = init_url + str(page_num)
	page_num += 1
	threading.Thread(target=fetch_html, args = (curr_url,)).start()

# entry
print u"""
---------------------
ver: 0.1
author: wzq
date: 2015-02-26
des: 爬取糗事百科,终端展示,q退出
---------------------
"""

usr_input=str(raw_input('press anykey start, q exit\n'))
while True:
	if usr_input == 'q':
		print 'exit now'
		break

	# global needShow
	needShow=True
	if not loading and len(items)< 60: # need load
		load()

	show()
	usr_input=str(raw_input(''))
